namespace CSharpPro._1013;
using System;
using System.Text.RegularExpressions;
public class user
{
   public static void Test()
    {
        bool isRunning = true;
        while (isRunning)
        {
            ShowMainMenu();
            string choice = Console.ReadLine()?.Trim() ?? "";
            
            switch (choice)
            {
                case "1":
                    PerformLogin();
                    break;
                case "2":
                    PerformRegistration();
                    break;
                case "3":
                    isRunning = false;
                    Console.WriteLine("系统已退出，再见！");
                    break;
                default:
                    Console.WriteLine("输入无效，请输入1-3之间的数字");
                    break;
            }
        }
    }
    
    static void ShowMainMenu()
    {
        Console.WriteLine("\n===== 用户中心 =====");
        Console.WriteLine("1. 登录");
        Console.WriteLine("2. 注册");
        Console.WriteLine("3. 退出");
        Console.Write("请选择操作: ");
    }
    
    static void PerformLogin()
    {
        Console.WriteLine("\n===== 账号登录 =====");
        
        string username;
        do
        {
            Console.Write("请输入用户名: ");
            username = Console.ReadLine()?.Trim() ?? "";
            if (string.IsNullOrEmpty(username))
                Console.WriteLine("用户名不能为空！");
        } while (string.IsNullOrEmpty(username));
        
        string password;
        do
        {
            Console.Write("请输入密码: ");
            password = Console.ReadLine()?.Trim() ?? "";
            if (string.IsNullOrEmpty(password))
                Console.WriteLine("密码不能为空！");
        } while (string.IsNullOrEmpty(password));
        
        Console.WriteLine($"\n登录成功！欢迎您，{username}");
        Console.WriteLine("已进入系统首页");
    }
    
    static void PerformRegistration()
    {
        Console.WriteLine("\n===== 账号注册 =====");
        
        string username = GetValidUsername();
        string password = GetValidPassword();
        VerifyPassword(password);
        
        Console.WriteLine("\n注册成功！可返回主菜单进行登录");
    }
    
    static string GetValidUsername()
    {
        while (true)
        {
            Console.Write("请设置用户名: ");
            string username = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不能为空！");
                continue;
            }

            if (username.Length < 3 || username.Length > 15)
            {
                Console.WriteLine("用户名长度必须为3-15个字符");
                continue;
            }

            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名只能包含字母、数字和下划线");
                continue;
            }

            return username;
        }
    }
    
    static string GetValidPassword()
    {
        while (true)
        {
            Console.Write("请设置密码: ");
            string password = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不能为空！");
                continue;
            }

            if (password.Length < 6 || password.Length > 20)
            {
                Console.WriteLine("密码长度必须为6-20个字符");
                continue;
            }

            bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
            bool hasLower = Regex.IsMatch(password, @"[a-z]");
            bool hasNumber = Regex.IsMatch(password, @"[0-9]");

            if (!hasUpper || !hasLower || !hasNumber)
            {
                Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字");
                continue;
            }

            return password;
        }
    }
    
    static void VerifyPassword(string password)
    {
        while (true)
        {
            Console.Write("请确认密码: ");
            string confirm = Console.ReadLine()?.Trim() ?? "";
            if (confirm == password)
                break;
            
            Console.WriteLine("两次输入的密码不一致，请重新输入");
        }
    }
}